<?php

namespace app\api\controller\user\server;

use app\api\controller\Controller;
use app\api\model\appointment\Service;
use app\api\model\server\Setting;
use app\api\model\server\User as DealerUserModel;
use app\api\model\server\Order as OrderModel;
use think\Db;
/**
 * 分销商订单
 * Class Order
 * @package app\api\controller\user\dealer
 */
class Order extends Controller
{
    /* @var \app\api\model\User $user */
    private $user;

    private $dealer;
    private $setting;

    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     */
    public function _initialize()
    {
        parent::_initialize();
        // 用户信息
        $this->user = $this->getUser();
        // 分销商用户信息
        $this->dealer = DealerUserModel::detail($this->user['user_id']);
        // 分销商设置
        $this->setting = Setting::getAll();
    }

    /**
     * 分销商订单列表
     * @param int $settled
     * @return array
     * @throws \think\exception\DbException
     */
    public function lists($settled = -1)
    {
        $model = new OrderModel;
        return $this->renderSuccess([
            // 提现明细列表
            'list' => $model->getList($this->user['user_id'], (int)$settled),
            // 页面文字
            'words' => $this->setting['words']['values'],
        ]);
    }


    /*
     * 用户服务订单
     */

    public function serverList()
    {
        $model = new Service;

        $list = $model->lists($this->user['user_id']);

        foreach($list as $k=>$item){
            $server_user = Db::name("server_user")->where(array("user_id"=>$item['service_id']))->find();
            $level = $server_user['level'];
            $goods = Db::name("goods")->where(array("goods_id"=>$item['user_goods_id']))->find();
            if($goods['level_set']){
                $levelset = json_decode($goods['level_set'],true);
                foreach($levelset as $key=>$val){

                    if($val['id'] == $level){
                        $service_price = $val['commission'] ? $val['commission'] : 1;
                        if($item['type_id'] == 7 || $item['type_id'] == 14){
                            $service = Db::name("appointment_service")->where(array("order_id"=>$item['order_id']))->find();
                            $list[$k]['service_price'] = $service_price*$service['use_time'];
                        }else{
                            $list[$k]['service_price'] = $service_price;
                        }
                    }
                }
            }
        }

        return $this->renderSuccess([
            // 提现明细列表
            'list' => $list
        ]);
    }

}